package main;

import game.Constants;
import habitat.Habitat;
import habitat.HabitatCreator;

import java.io.File;
import java.util.List;

import javax.swing.plaf.OptionPaneUI;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOpt;

import sim.Simulation;
import util.ResultFileWriter;

public class Main {

	public Habitat habitats[];
	public static void main(String[] args) {
		
		
		 OptionParser parser = new OptionParser("r:a:b:d:g:p:k:i:s:m:f:");
		 OptionSpec<Integer> r	 = parser.accepts( "r" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Double> a	 = parser.accepts( "a" ).withRequiredArg().ofType( Double.class );
		 OptionSpec<Double> b	 = parser.accepts( "b" ).withRequiredArg().ofType( Double.class );
		 OptionSpec<Integer> d	 = parser.accepts( "d" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Integer> g	 = parser.accepts( "g" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Integer> p	 = parser.accepts( "p" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Double> k	 = parser.accepts( "k" ).withRequiredArg().ofType( Double.class );
		 OptionSpec<Integer> i	 = parser.accepts( "i" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Integer> s	 = parser.accepts( "s" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<Integer> m	 = parser.accepts( "m" ).withRequiredArg().ofType( Integer.class );
		 OptionSpec<String> f		 = parser.accepts( "f" ).withOptionalArg().ofType( String.class );
		 OptionSpec<String> o		 = parser.accepts( "o" ).withOptionalArg().ofType( String.class );
		 

		 
		 OptionSet options = parser.parse(args);
		 
	
		 if( options.hasArgument(r) )
			 Constants.RUN_ID = (Integer)options.valueOf(r);
		 if( options.hasArgument(a) )
			 Constants.AVALUE = (Double)options.valueOf(a);
		 if( options.hasArgument(b) )
			 Constants.BVALUE = (Double)options.valueOf(b);
		 if( options.hasArgument(d) )
			 Constants.DIVERSITY_DISTR_TYPE = (Integer)options.valueOf(d);
		 if( options.hasArgument(g) )
			 Constants.GAME_ID = (Integer)options.valueOf(g);
		 if( options.hasArgument(p) )
			 Constants.NUM_OF_AGENTS = (Integer)options.valueOf(p);
		 if( options.hasArgument(k) ){
			 Constants.K1VALUE = (Double)options.valueOf(k);
			 Constants.K2VALUE = (Double)options.valueOf(k);
		 } 
		 if( options.hasArgument(i) )
			 Constants.NUM_OF_ITERATION = (Integer)options.valueOf(i);
		 if( options.hasArgument(s) )
			 Constants.STRATEGY_UPDATE = (Integer)options.valueOf(s);
		 if( options.hasArgument(m) )
			 Constants.MOBILITY = (Integer)options.valueOf(m);
		 if( options.hasArgument(f) )
			 Constants.TOPOLOGY_FILE_NAME = (String)options.valueOf(f);
		 if( options.hasArgument(o) )
			 Constants.OUTPUT_FILE_NAME = (String)options.valueOf(o);
		 else{
			 Constants.OUTPUT_FILE_NAME  = "R"+Constants.RUN_ID+"_A"+ Constants.AVALUE + "_B" + Constants.BVALUE  +"_M"+Constants.DIVERSITY_DISTR_TYPE;
		 }
		//util.ConfigReader.importConfigFile("config.ini");
		// / if generation type is 0,than create the topology with graphic mode
		// and run the simulation
		// manually
		if (Constants.MESH_GENERATION_TYPE == 1) {
			// / or create the topology from file
			HabitatCreator.getHabitatFactory().createContainerfromTopology(
					Constants.TOPOLOGY_FILE_NAME);
		}
		
		createOutputFile( Constants.OUTPUT_FILE_NAME );

		// /start simulation
		Simulation.getSimulation().startSimulation();

	}
	private static void createOutputFile(String logFileName) {
		// / create directory that log files will be stored
		File mainDir = new File("Logs");
		if (!mainDir.isDirectory())
			mainDir.mkdir();
		String outputFileName = mainDir.getAbsolutePath() + File.separator + logFileName + "_Data.csv";
		Simulation.logFileName =  mainDir.getAbsolutePath() + File.separator  +logFileName+"_Habitat_Data.csv";
		Simulation.output = new ResultFileWriter( outputFileName );
		//Simulation.loggerHabInfo = new FileLogger( logfilename2);

		if(Simulation.output.open()==false)
			System.exit(0);
//		if(Simulation.loggerHabInfo.open()==false)
//			System.exit(0);

	}
	

}
